// pages/home/live/live.js
var app = getApp;
const router = require('../../../config').router
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //页数
    lastPage: 0,
    pageNo: 1,
    currentTab: 0,
    showModal: false,
    navbar: [
      {
        id:'',
        name: '展会直播'
      }, {
        id: '',
        name: '重大活动直播'
      }
    ],
    banners: [
      {
        img: '../../../image/richMan_banner.png',
        content: '花开不谢——通草花'
      }
    ],
    list: [
      {
        img: '../../../image/home_list_1.png',
        title: '绿色发展  提升西瓜品质带绿色发展',
        content: '徐州市睢宁县魏集镇是著名的“西瓜”',
        date: '2018-08-24 17:50'
      }, {
        img: '../../../image/home_list_1.png',
        title: '绿色发展  提升西瓜品质带绿色发展',
        content: '徐州市睢宁县魏集镇是著名的“西瓜”',
        date: '2018-08-24 17:50'
      }
    ],
    oldList:[
      {
        img: '../../../image/home_list_1.png',
        title: '绿色发展  提升西瓜品质带绿色发展',
        content: '徐州市睢宁县魏集镇是著名的“西瓜”',
        date: '2018-08-24 17:50'
      }, {
        img: '../../../image/home_list_1.png',
        title: '绿色发展  提升西瓜品质带绿色发展',
        content: '徐州市睢宁县魏集镇是著名的“西瓜”',
        date: '2018-08-24 17:50'
      }
    ],
    windowHeight: '',
    windowWidth: '',
    height: '',
    navbarid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //首页轮播图
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'ad.query.banner',
        v: '1.0',
        format: 'json',
        categoryId: 9
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data.ad)
          that.setData({
            banners: res.data.ad
          })
        }
      }
    });
    //navbar
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'institutions.category.brand.recursion',
        v: '1.0',
        format: 'json',
        categoryId: 90
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          var category = res.data.category
          var navbarid = category[0].id;
          that.setData({
            navbar: category,
            navbarid: navbarid
          })
          
          //第一个分类
          wx.request({
            url: router,
            data: {
              appKey: 'w4q897jgvxkb',
              method: 'article.list.brand',
              v: '1.0',
              format: 'json',
              categoryId: navbarid
            },
            success: function (res) {
              if (res.statusCode == 200 && res.data.error != 'error') {
                var article = res.data.article
                if (article == undefined) {
                  article = []
                }
                console.log(article);
                that.setData({
                  oldList: article,
                  lastPage: res.data.page.last
                })
              }
            }
          })
        }
      }
    })
    
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    })
  },
  imageError(e) {
    var that = this
    var height = e.detail.height;
    var width = e.detail.width;
    var ratio = width / height
    var height1 = parseInt(that.data.windowWidth / ratio);
    console.log(height1)
    that.setData({
      height: height1 + 'px'
    })
  },
  // 开启弹窗
  btn: function () {
    this.setData({
      showModal: true
    })
  },
  // 关闭弹窗
  close: function () {
    this.setData({
      showModal: false
    })
  },
  previewImage: function (e) {
    var current = e.target.dataset.src;
    var imgalist = [];
    imgalist.push(current)
    wx.previewImage({
      current: current, // 当前显示图片的http链接 
      urls: imgalist // 需要预览的图片http链接列表 
    })
  },
  /**
   * 详情
   */
  articleClick: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/index?id=' + id
    })
  },
  navbarTap: function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'article.list.brand',
        v: '1.0',
        format: 'json',
        categoryId: id
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          var article = res.data.article
          if (article == undefined) {
            article = []
          }
          console.log(article);
          that.setData({
            oldList: article,
            lastPage: 0,
            pageNo: 1,
            navbarid: id,
            lastPage: res.data.page.last
          })
        }
      }
    })
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },
  //上拉加载
  onReachBottom() {
    var that = this;
    var p = that.data.pageNo
    var navbarid = that.data.navbarid
    p++
    var last = that.data.lastPage
    if (p <= last) {
      wx.request({
        url: router,
        data: {
          appKey: 'w4q897jgvxkb',
          method: 'article.list.brand',
          v: '1.0',
          format: 'json',
          categoryId: navbarid,
          pageNo: p
        },
        success: function (res) {
          if (res.statusCode == 200 && res.data.error != 'error') {
            // console.log(res.data)
            for (var i = 0; i < res.data.article.length; i++) {
              that.data.oldList.push(res.data.article[i])
            }
            setTimeout(function () {
              that.setData({
                oldList: that.data.oldList,
                pageNo: res.data.page.pageNo
              })
            }, 1000)
          }
        }
      });
    } else {
      wx.showToast({
        title: '没有更多。。',
        icon: 'loading',
        duration: 1500
      })
    }
  }
})